<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class TaskController extends AdminbaseController{

    /**
     * 任务列表
     */
    public function index(){
        $task = M("vruser1.task", null);
        $list = $task->select();
        $this->assign('list', $list);
        $this->display();
    }
    
    /**
     * 添加任务
     */
    public function add(){
        $id = I("id");
        $task = M("vruser1.task",null);
        $task_info = $task->where(array('id'=>$id))->find();
        $this->assign('task_info', $task_info);
        $this->assign('id', $id);
        $this->display();
    }

   /**
    * 添加或修改任务
    */
    public function add_task(){
        $task_name = I("task_name");
        $content   = I("content");
        $max_cnt   = I("max_cnt");
        $amount    = I("amount");
        $id        = I("id");

        $task = M("vruser1.task", null);
        $data['task_name'] = $task_name;
        $data['content']   = $content;
        $data['max_cnt']   = $max_cnt;
        $data['amount']    = $amount;
        if(!empty($id)){
            $task->where(array('id'=>$id))->save($data);
        }else{
            $task->add($data);
        }
        $this->success('添加成功！', U("Admin/Task/index"));
    }

    /**
     * 删除任务
     */

    public function delete(){
        $id = I("id");
        $task = M("vruser1.task",null);
        $task_info = $task->where(array('id'=>$id))->find();
        if(!$task_info){
            return $this->error("信息不存在");
        }
        $result = $task->where(array('id'=>$id))->delete();
        if ($result) {
            $this->success("删除成功！");
        } else {
            return $this->error($result['Message']);
        }
    }
}